

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>api.commander &mdash; Capture The Flag alpha documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     'alpha',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="Capture The Flag alpha documentation" href="../../index.html" />
    <link rel="up" title="Module code" href="../index.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">Capture The Flag alpha documentation</a> &raquo;</li>
          <li><a href="../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for api.commander</h1><div class="highlight"><pre>
<span class="c">#################################################################################</span>
<span class="c">#  This file is part of The AI Sandbox.</span>
<span class="c">#</span>
<span class="c">#  Copyright (c) 2007-2012, AiGameDev.com</span>
<span class="c">#</span>
<span class="c">#  Credits:         See the PEOPLE file in the base directory.</span>
<span class="c">#  License:         This software may be used for your own personal research</span>
<span class="c">#                   and education only.  For details, see the LICENSING file.</span>
<span class="c">#################################################################################</span>

<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">logging</span>


<div class="viewcode-block" id="Commander"><a class="viewcode-back" href="../../api.html#api.commander.Commander">[docs]</a><span class="k">class</span> <span class="nc">Commander</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    The base class for Commanders, that give orders to the team members.</span>
<span class="sd">    This class should be inherited from to create your own competition Commander.</span>
<span class="sd">    You must implement `tick(self)` in your custom Commander.</span>
<span class="sd">    &quot;&quot;&quot;</span>


<div class="viewcode-block" id="Commander.initialize"><a class="viewcode-back" href="../../api.html#api.commander.Commander.initialize">[docs]</a>    <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Use this function to setup your bot before the game starts.</span>
<span class="sd">        You can also set self.verbose = True to get more information about each bot visually.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">pass</span>

</div>
<div class="viewcode-block" id="Commander.tick"><a class="viewcode-back" href="../../api.html#api.commander.Commander.tick">[docs]</a>    <span class="k">def</span> <span class="nf">tick</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Override this function for your own bots.  Here you can access all the information in `self.game`,</span>
<span class="sd">        which includes game information, and `self.level` which includes information about the level.</span>
<span class="sd">        </span>
<span class="sd">        You can send commands to your bots using the `self.issue()` function in this class.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span>

</div>
<div class="viewcode-block" id="Commander.shutdown"><a class="viewcode-back" href="../../api.html#api.commander.Commander.shutdown">[docs]</a>    <span class="k">def</span> <span class="nf">shutdown</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Use this function to teardown your bot after the game is over.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">pass</span>

</div>
<div class="viewcode-block" id="Commander.issue"><a class="viewcode-back" href="../../api.html#api.commander.Commander.issue">[docs]</a>    <span class="k">def</span> <span class="nf">issue</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">CommandClass</span><span class="p">,</span> <span class="n">bot</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">dct</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Issue a command for a single bot, with optional arguments depending on the command.</span>

<span class="sd">        `CommandClass`: must be one of `[api.commands.Defend, api.commands.Attack, api.commands.Move]` </span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">verbose</span> <span class="ow">and</span> <span class="s">&#39;description&#39;</span> <span class="ow">in</span> <span class="n">dct</span><span class="p">:</span>
            <span class="k">del</span> <span class="n">dct</span><span class="p">[</span><span class="s">&#39;description&#39;</span><span class="p">]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">commandQueue</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">CommandClass</span><span class="p">(</span><span class="n">bot</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">dct</span><span class="p">))</span> 

</div>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">Commander</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        name        The name of this commander.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">log</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>         <span class="c"># TODO: This needs to correctly handle the proxy commander</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        log         The logging object that should be used for debug printing.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">handlers</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">module</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__module__</span><span class="p">]</span><span class="o">.</span><span class="n">__file__</span>
                <span class="n">filename</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">module</span><span class="p">),</span> <span class="s">&#39;logs&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="o">+</span><span class="s">&#39;.log&#39;</span><span class="p">)</span>
                <span class="n">output</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">FileHandler</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">addHandler</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">setLevel</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">IOError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="k">pass</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">verbose</span> <span class="o">=</span> <span class="bp">False</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        verbose     Set this to true to enable the display of the bot command descriptions next to each bot.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">level</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        level       The LevelInfo object describing the current level.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">game</span> <span class="o">=</span> <span class="bp">None</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        game        The GameInfo object describing this Commander&#39;s knowledge of the current state of the game.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">commandQueue</span> <span class="o">=</span> <span class="p">[]</span> <span class="c"># the queue were issues commands are stored to be run later by the game</span>


    <span class="k">def</span> <span class="nf">setGameInfo</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">info</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">game</span> <span class="o">=</span> <span class="n">info</span><span class="o">.</span><span class="n">game</span>


    <span class="k">def</span> <span class="nf">clearCommandQueue</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>        
        <span class="bp">self</span><span class="o">.</span><span class="n">commandQueue</span> <span class="o">=</span> <span class="p">[]</span>
</pre></div></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li><a href="../../index.html">Capture The Flag alpha documentation</a> &raquo;</li>
          <li><a href="../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2012, AiGameDev.com KG.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>